/**
 * 腾讯云身份验证工具模块
 */

const verifyIdentity = async (name, idCard) => {
  try {
    // 导入正确的身份验证模块
    const FaceidClient = require('tencentcloud-sdk-nodejs').faceid.v20180301.Client;
    
    // 实例化身份验证客户端
    const client = new FaceidClient({
      credential: {
        secretId: process.env.TENCENT_SECRET_ID,
        secretKey: process.env.TENCENT_SECRET_KEY
      },
      region: process.env.TENCENT_REGION || "ap-beijing",
      profile: {
        signMethod: "TC3-HMAC-SHA256",
        httpProfile: {
          reqMethod: "POST",
          reqTimeout: 30
        }
      }
    });
    
    // 调用身份证验证接口
    const params = {
      IdCard: idCard,
      Name: name
    };
    
    const data = await client.IdCardVerification(params);
    
    // 检查验证结果
    if (data.Result === "0") {
      return {
        success: true,
        message: "身份验证成功",
        data: data
      };
    } else {
      return {
        success: false,
        message: "身份信息不匹配，请检查姓名和身份证号",
        code: data.Result
      };
    }
    
  } catch (error) {
    console.error('身份验证服务调用失败:', error);
    
    // 如果是网络错误或服务不可用，可以选择跳过验证（根据业务需求）
    if (process.env.NODE_ENV === 'development') {
      console.warn('开发环境跳过身份验证');
      return {
        success: true,
        message: "开发环境跳过身份验证"
      };
    }
    
    return {
      success: false,
      message: "身份验证服务暂时不可用，请稍后重试"
    };
  }
};

module.exports = {
  verifyIdentity
};